package com.xyht.sca_s.student_manage_system.modules.appDynamic.controller;

import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.annotation.RepeatRequestCheck;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.entity.req.*;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.service.AppApplicationDataService;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.util.AppDynamicUtil;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 应用用户申请数据表：存放用户申请表数据 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-07-26
 */
@RestController
@RequestMapping("/appApplicationData")
@SecretBody
@RepeatRequestCheck
public class AppApplicationDataController {
    @Resource
    private AppApplicationDataService appApplicationDataService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;
    @Resource
    private AppDynamicUtil appDynamicUtil;

    @ApiModelProperty(value = "提交申请表数据并生成流程")
    @PostMapping("/submitData")
    public ResponseResult submitApplicationData(@RequestBody SubmitApplicationDataReq req, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return appApplicationDataService.submitApplicationData(req, userId);
    }

    @ApiModelProperty(value = "提交数据和流程生成申请表")
    @PostMapping("/submitProcess")
    public ResponseResult submitApplicationProcess(@RequestBody SubmitApplicationProcessReq req, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return appApplicationDataService.submitApplicationProcess(req, userId);
    }

    @ApiModelProperty(value = "撤销申请")
    @PostMapping("/cancel")
    public ResponseResult cancelApplication(@RequestBody ApplicationCancelReq cancelReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return appApplicationDataService.cancelApplication(cancelReq, userId);
    }

    @ApiModelProperty(value = "审批列表")
    @GetMapping("/list")
    public ResponseResult getApplicationList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                             @RequestParam(value = "applyType") Integer applyType,
                                             @RequestParam(value = "appId") String appId,
                                             HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return appApplicationDataService.getApplicationList(pageSize, pageNum, applyType, appId, userId);
    }

    @ApiModelProperty(value = "审批详情")
    @GetMapping("/detail")
    public ResponseResult getApplicationDetail(@RequestParam(value = "applicationId") String applicationId,
                                               @RequestParam(value = "appId") String appId,
                                               HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return appApplicationDataService.getApplicationDetail(applicationId, appId, userId);
    }

    @ApiModelProperty(value = "审批")
    @PostMapping("/allow")
    public ResponseResult allowApplication(@RequestBody ApplicationAllowReq allowReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return appApplicationDataService.allowApplication(allowReq, userId);
    }

    @ApiModelProperty(value = "转交")
    @PostMapping("/transmit")
    public ResponseResult transmitApplication(@RequestBody ApplicationTransmitReq transmitReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return appApplicationDataService.transmitApplication(transmitReq, userId);
    }

    @ApiModelProperty(value = "回退")
    @PostMapping("/back")
    public ResponseResult backApplication(@RequestBody ApplicationBackReq backReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return appApplicationDataService.backApplication(backReq, userId);
    }

    @ApiModelProperty(value = "操作记录")
    @GetMapping("/selectAll")
    public ResponseResult selectAllAppApply(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                            @RequestParam(value = "appId") String appId,
                                            @RequestParam(value = "applyUser", required = false) String applyUser,
                                            @RequestParam(value = "searchStr", required = false) String searchStr) {
        return appApplicationDataService.selectAllAppApply(pageNum, pageSize, appId, applyUser, searchStr);
    }

    @ApiModelProperty(value = "获取某个应用控件字段信息")
    @GetMapping("/getControlInfo")
    public ResponseResult getControlInfo(@RequestParam(value = "appId") String appId) {
        return appApplicationDataService.getControlInfo(appId);
    }

    @ApiModelProperty(value = "强制归档")
    @PostMapping("/force")
    public ResponseResult forceArchive(@RequestBody ApplicationBackReq backReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return appApplicationDataService.forceArchive(backReq, userId);
    }

    @ApiModelProperty(value = "批量导出")
    @PostMapping("/batch/export")
    public ResponseResult exportAllApplyList(@RequestBody AppDynamicExportReq exportReq, HttpServletResponse response) {
        return appDynamicUtil.exportApplyUtil(appApplicationDataService.exportAllApplyList(exportReq, response));
    }

    @ApiModelProperty(value = "模板下载")
    @PostMapping("/download")
    public ResponseResult tableDownload(@RequestParam(value = "applyId") String applyId, HttpServletResponse response) {
        return appDynamicUtil.exportApplyUtil(appApplicationDataService.tableDownload(applyId, response));
    }

    @ApiModelProperty(value = "强制流转")
    @PostMapping("/forceExchange")
    public ResponseResult forceExchange(@RequestBody ApplicationTransmitReq transmitReq) {
        return appApplicationDataService.forceExchange(transmitReq);
    }

    @ApiModelProperty(value = "代审批")
    @PostMapping("/insteadAllow")
    public ResponseResult insteadAllow(@RequestBody ApplicationAllowReq allowReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return appApplicationDataService.insteadAllow(allowReq, userId);
    }

    @ApiModelProperty(value = "强制驳回")
    @PostMapping("/forcedReject")
    public ResponseResult forcedReject(@RequestBody ApplicationBackReq backReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return appApplicationDataService.forcedReject(backReq, userId);
    }

    @ApiModelProperty(value = "删除单个申请表数据")
    @PostMapping("/deleteApplyData")
    public ResponseResult deleteApplyData(@RequestParam String applyId) {
        return appApplicationDataService.deleteApplyData(applyId);
    }

}

